﻿Namespace Mouse

    Module mMouse


        Public bLastFocusRef As Object
        Public HasChanged As Boolean

        Public Enum MouseInputType
            MouseMove = 0
            MouseClick = 1
            MouseDoubleClick = 2
            MouseUp = 3
            MouseDown = 4
        End Enum

        Public XCoord As Integer = 800 \ 2
        Public YCoord As Integer = 600 \ 2
        Public bIsDown As Boolean

        Public messageQueue As Queue

        Public Structure MouseMessage
            Dim type As MouseInputType
            Dim args As MouseEventArgs
        End Structure

        Public Enum MouseObjTypes
            Inventory = 0
        End Enum

        Public MovingObj As Object
        Public MouseObj As Object
        Public MouseObjXOffset As Integer, MouseObjYOffset As Integer
        Public MouseObjIndex As Integer = -1
        Public MouseObjType As MouseObjTypes

        Private Declare Function ShowCursor Lib "user32" (ByRef bShow As Integer) As Integer

        Public Sub HideMouse()
            'ShowCursor(False)
        End Sub

        Public Sub Init()
            messageQueue = New Queue
        End Sub

        Public Sub Shutdown()
            'ShowCursor(True)
        End Sub

        Public Sub DoInputs()

            SyncLock messageQueue

                Dim msg As MouseMessage
                Dim i As Integer
                Dim c As Integer = messageQueue.Count

                Do Until i = c

                    msg = messageQueue.Dequeue()

                    Select Case msg.type

                        Case MouseInputType.MouseDown
                            Mouse.bIsDown = True

                        Case MouseInputType.MouseUp
                            If Not MovingObj Is Nothing Then
                                Try
                                    MovingObj.UnFocus()
                                Catch ex As Exception
                                End Try
                                MovingObj = Nothing
                            End If
                            Mouse.bIsDown = False

                    End Select

                    'If Not MouseObj Is Nothing Then
                    'Windows.MouseInput(msg.type, msg.args)
                    'If msg.type = MouseInputType.MouseClick Then
                    'Try
                    'MouseObj.unfocus()
                    'catch ex As Exception
                    'End Try
                    'MouseObj = Nothing
                    'End If
                    'Else
                    Windows.MouseInput(msg.type, msg.args)
                    'End If

                    i += 1

                Loop

            End SyncLock

        End Sub

        Public Sub Input(ByRef type As MouseInputType, ByRef e As MouseEventArgs)

            If messageQueue Is Nothing Then Exit Sub

            Select Case type

                Case MouseInputType.MouseMove
                    Mouse.XCoord = e.X
                    Mouse.YCoord = e.Y

            End Select

            Dim msg As MouseMessage
            msg.type = type
            msg.args = e

            SyncLock messageQueue

                For Each tmp As MouseMessage In messageQueue
                    If tmp.type = msg.type Then
                        GoTo found
                    End If
                Next
                messageQueue.Enqueue(msg)

found:

            End SyncLock

            'HasChanged = True

        End Sub

        Public Sub LostFocus()

            Mouse.bIsDown = False

        End Sub

        Public Sub SetHoverFlag(ByRef rect As Rectangle, ByRef flag As Boolean)

            flag = rect.Contains(Mouse.XCoord, Mouse.YCoord)

        End Sub

        Public Sub SetFocusFlag(ByRef rect As Rectangle, ByRef flag As Boolean)

            If bIsDown Then
                SetHoverFlag(rect, flag)
                If flag Then

                    If Not bLastFocusRef Is Nothing Then
                        Try
                            bLastFocusRef = False
                        Catch
                        End Try
                    End If
                    bLastFocusRef = flag

                End If
            End If

        End Sub

        Public Function IsOver(ByRef rect As Rectangle) As Boolean

            'If IsClicking Then

            Dim tmp As Boolean
            SetHoverFlag(rect, tmp)
            Return tmp

            'End If
            'Return False

        End Function

    End Module

End Namespace
